package com.zdft.promotion.common.acl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2019/2/22.
 */
public class URLAccessControlFilter implements Filter
{
    private static final Logger log = LoggerFactory.getLogger(URLAccessControlFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String method = request.getMethod().toUpperCase();
        String url = StringUtils.removeStart(request.getRequestURI(), request.getContextPath());
        log.info("Check for method:{}, url:{}...", method, url);
        filterChain.doFilter(servletRequest, servletResponse);
//        servletResponse.setContentType("application/json;charset=utf8");
//        try (OutputStream stream = servletResponse.getOutputStream())
//        {
//            stream.write(JSON.toJSONBytes(new RestfulResult("-9999", "您无该资源访问权限。")));
//            stream.flush();
//        }
        //todo
    }

    @Override
    public void destroy()
    {
    }
}
